package util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUtil {
    private static String url;
    private static String name;
    private static String password;

    static {
        try {
        Properties pro=new Properties();
        InputStream is=JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        pro.load(is);

        String diverClass=pro.getProperty("diverClass");
        Class.forName(diverClass);
        url=pro.getProperty("url");
        name=pro.getProperty("name");
        password=pro.getProperty("password");

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

 public static Connection getConnection(){
     Connection con=null;
     try {
         con= DriverManager.getConnection(url,name,password);
     } catch (SQLException e) {
         e.printStackTrace();
     }
     return con;
 }
    public static void close(Connection con, PreparedStatement psm, ResultSet rs){
        if(con != null)
        {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(psm != null)
        {
            try {
                psm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(rs != null)
        {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
 }

}
